package org.zttc.itat.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zttc.itat.model.News;
import org.zttc.itat.model.Pager;
import org.zttc.itat.model.Sort;
import org.zttc.itat.model.SortType;
import org.zttc.itat.service.INewsService;
import org.zttc.itat.service.ISortService;
import org.zttc.itat.service.ISortTypeService;
import org.zttc.itat.util.ActionUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("xinwenAction")
@Scope("prototype")
public class XinwenAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int sortId;
	private int newsId;
	private int sortTypeId;
	@Resource(name="sortService")
	private ISortService sortService;
	@Resource(name="sortTypeService")
	private ISortTypeService sortTypeService;
	@Resource(name="newsService")
	private INewsService newsService;
	public int getSortId() {
		return sortId;
	}

	public void setSortId(int sortId) {
		this.sortId = sortId;
	}
	
	public int getSortTypeId() {
		return sortTypeId;
	}
	public void setSortTypeId(int sortTypeId) {
		this.sortTypeId = sortTypeId;
	}
	

	public String init(){
		List<Sort> sortList = sortService.listAllSort();
		for(Sort sort:sortList){
			List<News> newslist = newsService.list(sort.getId());
			ActionContext.getContext().put(sort.getEnglishfield()+"list", newslist);
		}
		List<SortType> sortTypeList = sortTypeService.listAllSortType();
		for(SortType sortType:sortTypeList){
			List<News> newslist = newsService.list(sortType.getId());
			ActionContext.getContext().put(sortType.getEnglishfield()+"list", newslist);
		}
		ActionContext.getContext().put("sortlist",sortService.listAllSort());
		return SUCCESS;
	}
	
	public String list(){
		String url =null;
		if(sortId>0){
		  Sort sort = sortService.load(sortId);
		  url ="front_xinwen_"+sort.getEnglishfield()+".action?sortId="+sortId;
		}else {
		   SortType sortType = sortTypeService.load(sortTypeId);
		   url ="front_xinwen_"+sortType.getEnglishfield()+".action?sortId="+sortId;
		}
		ActionUtil.setUrl(url);
		return ActionUtil.REDIRECT;
	}
	/**
	 * 根据新闻id获取新闻
	 * @return
	 */
	public String detail(){
		News news =(News)newsService.load(newsId);
		Sort sort =(Sort)sortService.load(news.getSort().getId());
		ActionContext.getContext().put("news",news);
		ActionContext.getContext().put("sort",sort);
		this.setSortNews(2);
	    this.setSortNews(4);
		return SUCCESS;
	}
	/**
	 * 获取一级栏目的子栏目数集
	 * @author zxit
	 * */
	public void setSortList(int sortId){
		List<SortType> list = sortService.listScopeSort(sortId);
		if(list.size()>0){
		  ActionContext.getContext().put("list",list);
		}
	}
	/**
	 * 获取一级栏目所有的新闻
	 * @author zxit
	 * */
	public void setSortNews(int sortId){
		Sort sort = sortService.load(sortId);
		if(sort!=null){
	      ActionContext.getContext().put(sort.getEnglishfield()+"list",newsService.list(sort.getId()));
		}
	}
	

	/**
	 * 获取二级栏目的所有新闻
	 * @author zxit
	 * */
	public void setSortTypeNews(int sortTypeId){
		SortType sortType = sortTypeService.load(sortTypeId);
		if(sortType!=null){
	      ActionContext.getContext().put(sortType.getEnglishfield()+"list",newsService.list(sortType.getId()));
		}
	}
	/**
	 * 商会新闻
	 * @author zxit
	 * */
	public String news(){
        this.setSortList(sortId);
        this.setSortNews(sortId);
        this.setSortNews(2);
		return SUCCESS;
	}
	
	/**
	 * 商会简介
	 * @author weilizong
	 * */
	public String introduce(){
        this.setSortList(sortId);
        //this.setSortNews(sortId);
        this.setSortNews(1);
		return SUCCESS;
	}
	
	/**
	 * 商会章程
	 * @author weilizong
	 * */
	public String rule(){
        this.setSortList(sortId);
        //this.setSortNews(sortId);
        this.setSortNews(1);
		return SUCCESS;
	}
	
	
	/**
	 * 商会成员
	 * @author weilizong
	 * */
	public String member(){
        this.setSortList(sortId);
        //this.setSortNews(sortId);
        this.setSortNews(1);
		return SUCCESS;
	}
	
	/**
	 * 商会活动
	 * @author zxit
	 * */
	public String event(){
        this.setSortList(sortId);
        this.setSortNews(sortId);
        this.setSortNews(5);
		return SUCCESS;
	}
	/**
	 * 商会交流
	 * @author zxit
	 * */
	public String social(){
		 this.setSortList(sortId);
	     this.setSortNews(sortId);
	     this.setSortNews(2);
	     return SUCCESS;
	}
	/**
	 * 渝商动态
	 * @author zxit
	 * */
	public String personevent(){
		this.setSortList(sortId);
	    this.setSortNews(sortId);
	    this.setSortNews(2);
	    return SUCCESS;
	}
	/**
	 * 商会公告
	 * @author zxit
	 * */
	public String notice(){
		this.setSortList(sortId);
	    this.setSortNews(sortId);
	    this.setSortNews(2);
	    return SUCCESS;
	}
	/**
	 * 会员信息
	 * @author zxit
	 * */
	public String trade(){
		this.setSortList(sortId);
	    this.setSortNews(sortId);
	    this.setSortNews(2);
	    this.setSortNews(4);
	    return SUCCESS;
	}
	/**
	 * 外部资源
	 * */
	public String outsidetrade(){
		this.setSortList(sortId);
	    this.setSortNews(sortId);
	    this.setSortNews(2);
	    this.setSortNews(4);
	    return SUCCESS;
	}	
	
	/**
	 * 外部资源
	 * */
	public String join(){
		this.setSortList(sortId);
	    //this.setSortNews(sortId);
	    this.setSortNews(2);
	    this.setSortNews(4);
	    return SUCCESS;
	}	
	
	public String mien(){
		this.setSortList(sortId);
	    //this.setSortNews(sortId);
	    this.setSortNews(2);
	    this.setSortNews(4);
	    return SUCCESS;
	}

	public int getNewsId() {
		return newsId;
	}

	public void setNewsId(int newsId) {
		this.newsId = newsId;
	}	
}
